﻿using UnityEngine;

using UnityEngine.EventSystems;

namespace demo3
{
    public enum CursorIconType{
        None,
        Rotate_3D,

        Rotate_forward,
        Move
    }
    public class CameraAreaUI : MonoBehaviour
    {
        public GameObject CameraArea_rotate;
        public bool isMove = false;
        public bool isHover = false;
        Vector2 DragDelVector = Vector2.zero;
        public Transform target;
        //float PinchDis = 0;

        public Camera mainCamera;
        
        public Texture2D icon_3dRotate;
        public Texture2D icon_forwardRotate;
        public Texture2D icon_move;
        
        public CursorIconType currentIconType = CursorIconType.None;

        private float distance = 10f;               //摄像机与目标物体距离
        void Start(){
            //Texture2D img = (Texture2D)Resources.Load("Textures/img");
            //Cursor.SetCursor(icon_3dRotate,Vector2.zero,CursorMode.Auto);//默认鼠标样式更改为img图片
            //Cursor.SetCursor(null,Vector2.zero,CursorMode.Auto);//默认鼠标样式
            CloneTarget.position = target.position;
			CloneTarget.rotation = target.rotation;

            Vector3 position = CloneTarget.position - (mainCamera.transform.rotation * Vector3.forward * distance);
			mainCamera.transform.position = position;
        }

        void SetCursor(CursorIconType type){
            if(currentIconType == type) return;
            if(type == CursorIconType.None){
                Cursor.SetCursor(null,Vector2.zero,CursorMode.Auto);
            }else if(type == CursorIconType.Rotate_3D){
                Cursor.SetCursor(icon_3dRotate,Vector2.zero,CursorMode.Auto);
            }else if(type == CursorIconType.Rotate_forward){
                Cursor.SetCursor(icon_forwardRotate,Vector2.zero,CursorMode.Auto);
            }else if(type == CursorIconType.Move){
                Cursor.SetCursor(icon_move,Vector2.zero,CursorMode.Auto);
            }
            currentIconType = type;

        }
        public void OnPointerEnter()
        {
            isHover = true;
            print("------OnPointEnter ");

        }

        public void OnPointerExit()
        {
            isHover = false;
            print("-----OnPointerExit");

        }
        public void OnPointerDown(BaseEventData p)
        {
            PointerEventData eventData = p as PointerEventData;
            print("++++++OnPointerDown "+eventData.position.ToString());
        }
        public void OnPointerUp(BaseEventData p)
        {
            print("++++++OnPointerUp");
        }

        bool is3DRoate = false;
        //开始拖拽
        public void OnBeginDrag(BaseEventData p)
        {
           
            PointerEventData eventData = p as PointerEventData;
             print("******OnBeginDrag "+eventData.pointerDrag);
            if (eventData.pointerDrag == CameraArea_rotate && (Input.touchCount == 1 || Input.GetMouseButton(1))) {
                isMove = true;
               
                Vector2 center = new Vector2(Screen.width/2.0f,Screen.height/2.0f);
                if(Vector3.Distance(eventData.position,center)<(Screen.height/2.0)){
                     SetCursor(CursorIconType.Rotate_3D);
                     is3DRoate = true;
                }else
                {
                    SetCursor(CursorIconType.Rotate_forward);
                    is3DRoate = false;
                }
           }
        }
        Vector2 lastMouse = Vector2.zero;
        //拖拽中
        public void OnDrag(BaseEventData p)
        {
            
            PointerEventData eventData = p as PointerEventData;
            print("******OnDrag "+eventData.pointerDrag.name);
            if(!(Input.touchCount == 1 || Input.GetMouseButton(1))){return;}//鼠标右键
            if (eventData.dragging)
            {
                isMove = true;
            }
            else
            {
                isMove = false;
            }
            
            if(lastMouse== Vector2.zero){
                lastMouse = eventData.position;
                return;
            }

            Vector2 currentMouse = eventData.position;
            if(is3DRoate){
                Vector3 deltaPos = Camera.main.ScreenToWorldPoint(new Vector3(currentMouse.x,currentMouse.y,10))-Camera.main.ScreenToWorldPoint(new Vector3(lastMouse.x,lastMouse.y,10));

                mainCamera.transform.RotateAround(CloneTarget.position,
                                                    -Vector3.Cross(deltaPos,mainCamera.transform.forward),Time.deltaTime*360);
            }else
            {
                float s = Vector2.Dot(eventData.delta,Vector3.down);
                mainCamera.transform.RotateAround(CloneTarget.position,
                                                   mainCamera.transform.forward,Mathf.Sign(s) * Time.deltaTime*180);
            }
            lastMouse = currentMouse;
            
        }
        


        //拖拽结束
        public void OnEndDrag(BaseEventData p)
        {
            
            PointerEventData eventData = p as PointerEventData;
            print("******OnEndDrag "+eventData.pointerDrag.name);
            lastMouse = Vector3.zero;
            if (eventData.pointerDrag == CameraArea_rotate) {
                isMove = false;
                SetCursor(CursorIconType.None);
            }
        }

        void Update(){
            Zoom();
            Move();
        }

        private Vector2 oldPosition1 = Vector2.zero;
        private Vector2 oldPosition2 = Vector2.zero;
        public void Zoom()
        {
            if (Input.touchCount >= 1)
            {
                if (Input.touchCount >= 2)
                {

                    if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
                    {
                        Vector2 tempPosition1 = Input.GetTouch(0).position;
                        Vector2 tempPosition2 = Input.GetTouch(1).position;

                        if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
                        {
                            //UpdatePinchMove(0.8f);
                            UpdatePinch(0.8f);
                        }
                        else
                        {
                            //UpdatePinchMove(-0.08f);
                            UpdatePinch(-0.08f);
                        }
                        oldPosition1 = tempPosition1;
                        oldPosition2 = tempPosition2;
                    }
                }

            }
            else
            {

                float pinch = Input.GetAxis("Mouse ScrollWheel");
                UpdatePinch(pinch);
            }
        }
        
		//////////////////////////////////////////////////摄像机放大，放小//////////////////////////////////////////////////
		

		private float fov_distance = 60f;               //摄像机与目标物体距离
		private float fov_scrollSpeed = 45;				//缩放速度
		private float fov_Minzoom = 2;					//缩放最小值
		private float fov_Maxzoom = 60;					//缩放最大值

		private float fov_distanceLerp = 60;
		private float fov_distance_Max;
		///////////////////////////////////////////////
        void UpdatePinch(float pinch)
        {

            if (!isHover)
                return;
            if (Input.touchCount == 2 || Input.mouseScrollDelta != Vector2.zero)
            {
                //if(wowcamera!=null)
                //    wowcamera.UpdatePinch(pinch);
                fov_distance = Mathf.Lerp(fov_distance,fov_distanceLerp,Time.deltaTime * fov_scrollSpeed);

                fov_distanceLerp = Camera.main.fieldOfView;
                //Debug.LogError("2 fov_distanceLerp:"+fov_distanceLerp);
                float fov_t = fov_distanceLerp -pinch*fov_scrollSpeed ;
                //Debug.LogError("3 fov_distanceLerp:"+fov_t);
                fov_distanceLerp = Mathf.Clamp(fov_t, fov_Minzoom, fov_Maxzoom);
                //Debug.LogError("4 fov_distanceLerp:"+fov_distanceLerp);

                // m_c = fov_distanceLerp/fov_distance_Max;

                // xSpeed = Mathf.Lerp(rotationSpeed_fov_Min,rotationSpeed_Min,m_c);
                // ySpeed = xSpeed;

                // m_xSpeed = Mathf.Lerp(moveSpeed_fov_Min,moveSpeed_Min,m_c);
                // m_ySpeed = m_xSpeed;

                Camera.main.fieldOfView = fov_distance;
            }
        }
        private bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
        {

            float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
            float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
            if (leng1 < leng2)
            {

                return true;
            }
            else
            {

                return false;
            }

        }
        //////////////////////////////////////////////////移动摄像机//////////////////////////////////////////////////
		public float m_xSpeed = -5f;
		public float m_ySpeed = -5f;

		float moveSpeed_Max = -5;
		float moveSpeed_Min = -2f;

		float moveSpeed_fov_Min = -0.1f;

		//private float targetHeight = 0;           //与目标物体的高度差，eg:1.7 摄像机比目标高1.7米
		//public Transform target;
		//public Transform cameraTrans;
		public Transform CloneTarget;
        
        void Move(){
            if(Input.GetMouseButton(2)){//鼠标中键 控制拖拽
                SetCursor(CursorIconType.Move);
				float x = Input.GetAxis("Mouse X") * m_xSpeed *0.02f;
				float y = Input.GetAxis("Mouse Y") * m_ySpeed *0.02f;
				Vector3 targetPos = CloneTarget.position + mainCamera.transform.right * x + mainCamera.transform.up * y;
				CloneTarget.position = targetPos;

                Vector3 position = CloneTarget.position - (mainCamera.transform.rotation * Vector3.forward * distance);
			    mainCamera.transform.position = position;
			}
            if(Input.GetMouseButtonUp(2)){
                SetCursor(CursorIconType.None);
            }
        }
        
       

    }

    
}
